import {
  hasChineseReg,
  hasEnglishReg,
  hasJapaneseReg,
  hasTailReg,
  hasKoreanReg,
} from './regexp'

const getCount = (content: string, reg: RegExp) => {
  let count = 0
  for (let i = 0; i < content.length; i++) if (reg.test(content[i])) count++
  return count
}

export default [
  {
    name: 'english',
    iso: 'en',
    label: '英文',
    check(content: string) {
      const count = getCount(content, hasEnglishReg)
      return {
        count,
        percent: Number((count * 100 / content.length).toFixed(2)),
        iso: 'en',
      }
    },
  },
  {
    name: 'chinese',
    iso: 'zh',
    label: '中文',
    check(content: string) {
      const count = getCount(content, hasChineseReg)
      return {
        count,
        percent: Number((count * 100 / content.length).toFixed(2)),
        iso: 'zh',
      }
    },
  },
  {
    name: 'japanese',
    label: '日语',
    iso: 'ja',
    check(content: string) {
      const count = getCount(content, hasJapaneseReg)
      return {
        count,
        percent: Number((count * 100 / content.length).toFixed(2)),
        iso: 'ja',
      }
    },
  },
  {
    name: 'tail',
    iso: 'th',
    label: '泰语',
    check(content: string) {
      const count = getCount(content, hasTailReg)
      return {
        count,
        percent: Number((count * 100 / content.length).toFixed(2)),
        iso: 'th',
      }
    },
  },
  {
    name: 'korean',
    iso: 'ko',
    label: '韩语',
    check(content: string) {
      const count = getCount(content, hasKoreanReg)
      return {
        count,
        percent: Number((count * 100 / content.length).toFixed(2)),
        iso: 'ko',
      }
    },
  },
]
